<?php
class cj extends CI_Controller {
	function index() {
		$this -> pub -> logp('浏览抽奖', '1-2');
		$da = $this -> pub -> get_setting('抽奖', 'cj');
		$this -> load -> view('adminx/cj', $da);
	}

	function add() {
		$this -> pub -> logp('添加抽奖', '1-2');
		if ($_POST) {
			$da['type'] = $this -> input -> post('type');
			$da['state'] = $this -> input -> post('state');
			$da['name'] = $this -> input -> post('name');
			$da['jj'] = $this -> input -> post('jj');
			$da['pic'] = $this -> input -> post('pic');
			$da['min'] = $this -> input -> post('min');
			$da['max'] = $this -> input -> post('max');
			$da['nr'] = $this -> input -> post('nr');
			$da['sort'] = $this -> input -> post('sort');
			
			$this -> pub -> insert('cj', $da);
			$this -> pub -> logx('添加抽奖 - ' . $da['title']);
			echo json_encode(array('type' => 1));
		} else {
			$da = $this -> pub -> get_setting('抽奖', 'cj');
			$this -> load -> view('adminx/cj_add', $da);
		}
	}

	function up($idx) {
		$this -> pub -> logp('修改抽奖 - ' . $idx, '1-2');
		if ($_POST) {
			$da['mx']['type'] = $this -> input -> post('type');
			$da['mx']['state'] = $this -> input -> post('state');
			$da['mx']['name'] = $this -> input -> post('name');
			$da['mx']['jj'] = $this -> input -> post('jj');
			$da['mx']['pic'] = $this -> input -> post('pic');
			$da['mx']['min'] = $this -> input -> post('min');
			$da['mx']['max'] = $this -> input -> post('max');
			$da['mx']['nr'] = $this -> input -> post('nr');
			$da['mx']['sort'] = $this -> input -> post('sort');
			
			$this -> db -> update('cj', $da['mx'], array('wz_id' => $idx));
			$da['msg'] = '修改抽奖成功';
			$da['title'] = '修改抽奖';
			echo json_encode(array('type' => 1));
		} else {
			$q = $this -> db -> get_where('cj', array('id' => $idx));
			if ($q -> num_rows() > 0) {
				$da['mx'] = $q -> row_array(0);
				$da['msg'] = '';
				$da['title'] = '修改抽奖';
				$this -> load -> view('adminx/cj_up', $da);
			} else {
				show_error('您的请求不存在！！');
			}
		}
	}

	function get() {
		$this -> pub -> logp('获取抽奖信息', '1-2');
		if ($this -> input -> post('key')) {
			$this -> db -> select('count(*)');

			$this -> db -> like('name', $this -> input -> post('key'));
			$q = $this -> db -> get_where('cj');
			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
			$this -> db -> like('name', $this -> input -> post('key'));
		} else {
			$this -> db -> select('count(*)');
			$q = $this -> db -> get_where('cj');
			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
		}
		//一共的页数
		$da['num'] = ceil($num / 30);

		$page = is_numeric($this -> input -> post('page')) && $this -> input -> post('page') > 0 ? $this -> input -> post('page') : 1;
		$page = $page > $da['num'] ? $da['num'] : $page;
		$da['page'] = $page;

		$page = ($page - 1) == -1 ? 0 : ($page - 1);

		$this -> db -> limit(30, $page * 30);

		$this -> db -> order_by('sort', 'asc');
		$q = $this -> db -> get_where('cj');
		$da['content'] = '';
		foreach ($q->result_array() as $v) {
			$da['content'] .= '<tr>';
			$da['content'] .= '<td>' . $v['id'] . '</td>';
			$da['content'] .= '<td>' . $v['name'] . '</td>';
			$da['content'] .= '<td><a href="/iadmin/cj/up/' . $v['id'] . '" type="button" class="btn default green"><i class="fa fa-edit"></i> 修改</a>  </td>';
			$da['content'] .= '</tr>';
		}
		$da['type'] = 1;
		echo json_encode($da);
	}

}
?>